/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server;

import commom.Host;
import commom.VirtualMachine;
import java.util.ArrayList;
import java.util.UUID;

/**
 *
 * @author gravena
 */
public class Slave extends Host {
    /**
     * List of available Virtual Machines on a host
     */
    private ArrayList<VirtualMachine> vms;
    private long checkSum;

    public Slave(String ip, int port, String name, boolean active) {
        super(ip, port, name, active);
        this.vms = new ArrayList<>();
        //this.calculateCheckSum();
        this.checkSum = 0;
    }

    final public void calculateCheckSum() {
        VirtualMachine vm;
        long sum = 0;

        for(int i=0;i<this.vms.size();i++) {
            vm = this.vms.get(i);
            String name = vm.getName();
            for(int j=0;j<name.length();j++) {
                sum += name.charAt(j);
            }
        }

        this.checkSum = sum;        
    }

    public long getCheckSum() {
        return this.checkSum;
    }

    public static long getCheckSum(ArrayList<VirtualMachine> vms) {
        VirtualMachine vm;
        long sum = 0;

        for(int i=0;i<vms.size();i++) {
            vm = vms.get(i);
            String name = vm.getName();
            for(int j=0;j<name.length();j++) {
                sum += name.charAt(j);
            }
        }

        return sum;
    }

    public boolean addVM(VirtualMachine vm) {
        return this.vms.add(vm);
    }

    public VirtualMachine getVM(int i) {
        return this.vms.get(i);
    }

    public int getVMSize() {
        return this.vms.size();
    }

    public void setVMList(ArrayList<VirtualMachine> vms) {
        this.vms = vms;
    }
    
    public ArrayList<VirtualMachine> getVMList() {
        return this.vms;
    }
    
    public VirtualMachine searchByUUID(UUID id) {
        for(int i=0;i<this.vms.size();i++) {
            if(id.compareTo(this.vms.get(i).getUUID()) == 0) {
                return this.vms.get(i);
            }
        }
        return null;
    }

    
}
